package com.top.up.core.config.executor;

import com.top.up.constants.ExecutorNames;
import com.top.up.constants.ExecutorProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@ConfigurationProperties(prefix = "spring.pool")
public class ExecutorConfig {

    private ExecutorProperties primary = new ExecutorProperties();


    @Bean(ExecutorNames.PRIMARY_EXECUTOR)
    public ThreadPoolTaskExecutor primaryThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(primary.getCorePoolSize());
        executor.setMaxPoolSize(primary.getMaxPoolSize());
        executor.setQueueCapacity(primary.getQueueCapacity());
        executor.setThreadNamePrefix("primaryThread-");
        // 拒绝策略，默认
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }

    public void setPrimary(ExecutorProperties primary) {
        this.primary = primary;
    }

}
